import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    bbs_class_list: [],
    userinfo: {
      id: 0
    },
    login_show: false,
    push_bbs_list: [],
    like_bbs_list: [],
    hot_bbs_list: [],
    good_bbs_list: [],
    bbs_list: {}
  },
  mutations: {
    setPushBbsList(state, value) {
      let data = {
        type: 'push_bbs_list',
        list: value
      }
      this.commit('setBbsListByType', data);
    },
    setLikeBbsList(state, value) {
      let data = {
        type: 'like_bbs_list',
        list: value
      }
      this.commit('setBbsListByType', data);
    },
    setHotBbsList(state, value) {
      let data = {
        type: 'hot_bbs_list',
        list: value
      }
      this.commit('setBbsListByType', data);
    },
    setGoodBbsList(state, value) {
      let data = {
        type: 'good_bbs_list',
        list: value
      }
      this.commit('setBbsListByType', data);
    },
    setBbsListByType(state, value) {
      let list = state[value.type];

      if (value.list.current_page > 1) {
        list.current_page = value.list.current_page;
        list.last_page = value.list.last_page;
        list.per_page = value.list.per_page;
        list.total = value.list.total;
        list.data.push(value.list.data);
      }
      list = value.list;
      state[value.type] = list;

    },
    setBbsList(state, value) {
      console.log(value);
      if (!state.bbs_list[value.type] || value.list.current_page == 1) {
        // state.bbs_list[value.type] = {};
        Vue.set(state.bbs_list, value.type, value.list);
        // state[value.type] = value.list;
      } else {
        state.bbs_list[value.type].current_page = value.list.current_page;
        state.bbs_list[value.type].last_page = value.list.last_page;
        state.bbs_list[value.type].per_page = value.list.per_page;
        state.bbs_list[value.type].total = value.list.total;
        for (let i = 0; i < value.list.data.length; i ++) {
          state.bbs_list[value.type].data.push(value.list.data[i]);

        }
      }
      console.log(state.bbs_list[value.type]);
    },
    loginShow(state) {
      state.login_show = !state.login_show;
    },
    setUserinfo(state, value) {
      state.userinfo = value;
    },
    setBbsClassList(state, value) {
      state.bbs_class_list = value;
    }
  },
  actions: {
    getBbsList({ commit }, data) {
      if (!data) {
        data = { type: 0 }
      }
      return new Promise((resolve, reject) => {
        Vue.axios
        .get("/api/index/bbs/list", {
          params: {
            type: data.type,
            page: data.page
          }
        })
        .then(res => {
          let param_data = {
            type: data.type,
            list: res.data
          };
          if (res.data.current_page < res.data.last_page) {
            commit("setBbsList", param_data);
            resolve();
          } else {
            reject();
          }
        });
      });
      
      // Vue.axios.get("/api/index/bbs/list", { params: data }).then(res => {
      //   if (data.type == 0) {
      //     commit("setPushBbsList", res.data);
      //   } else if (data.type == 1) {
      //     commit("setLikeBbsList", res.data);
      //   } else if (data.type == 2) {
      //     commit("setHotBbsList", res.data);
      //   } else {
      //     commit("setGoodBbsList", res.data);
      //   }
      // });
    },
    register({ commit }, data) {
      Vue.axios.post("/api/index/bbs/list", data).then(res => {
        // this.list = res.data;
        commit("setLikeBbsList", res.data);
      });
    },
    getUserinfo({ commit }) {
      Vue.axios.post("/api/index/user/info").then(res => {
        // this.list = res.data;
        if (!res.data.err) {
          commit("setUserinfo", res.data);
        }
      });
    },
    getBbsClassList({ commit }) {

      Vue.axios.post("/api/index/bbs/bbsList").then(res => {
        // this.list = res.data;
        if (!res.data.err) {
          commit("setBbsClassList", res.data);
        }
      });
    }
  },
  getters: {
    getBbsListByType: (state) => (type) => {
      return state.bbs_list[type] || {
        current_page: 0,
        last_page: 0,
        per_page: 0,
        total: 0,
        data: []
      };
    }
  }
})
